<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/7/16
 * Time: 16:01
 * Created by Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin\Store;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\OrderIndexRequest;
use App\Http\Requests\Admin\Store\Youzan\SyncOrdersRequest;
use App\Models\Customer\Customer;
use App\Models\Order;
use App\Models\Store\Store;
use App\Services\Utils\Guard;
use App\Services\Youzan\OrderRepository;
use App\Services\Youzan\OrderService;
use Illuminate\Http\Request;

class OrderController extends ApiController
{
    const ROUTES = [
        'sync' => 'api.admin.store.order.sync',
        'index' => 'api.admin.store.order.index',
        'show' => 'api.admin.store.order.show',
    ];

    protected $orderService;
    protected $orderRepository;

    public function __construct(OrderService $orderService, OrderRepository $orderRepository)
    {
        $this->orderService = $orderService;
        $this->orderRepository = $orderRepository;
    }


    /**
     * 同步订单到orders表.
     *
     * @param SyncOrdersRequest $request
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Throwable
     */
    public function syncOrders(SyncOrdersRequest $request)
    {
        $request->checkTimeSpan();
        /** @var Store $store */
        $store = Guard::store();

        $youzanAuthorizer = $store->youzanAuthorizer;

        $this->checkIsTrue($youzanAuthorizer,trans('admin.no_auth_to_youzan'));

        $trades_sold_params = $this->orderRepository->assembleTradesSoldGetParams(
            1, 100, $request->start_created, $request->end_created
        );

        //  获取某一时间段(默认过去一周)的所有订单
        $list = $this->orderRepository->getAllTradesSold($youzanAuthorizer, $trades_sold_params);

        // 格式化数据
        $transformList = $this->orderService->transformOrderLists($list);

        // 同步订单到orders表
        $sync = $this->orderRepository->syncOrders($transformList);

        return $this->response(compact('sync'));
    }

    /**
     *  后台店铺 查看有赞订单列表
     * @test
     *
     * @param OrderIndexRequest $request
     * @return array|\Illuminate\Http\Response
     *
     * @throws \Exception
     */
    public function index(OrderIndexRequest $request)
    {
        /** @var Store $store */
        $store = Guard::store();
            $this->checkIsTrue($store->youzan_authorizer_id, trans('admin.youzan-shop'));

        $orders = $this->orderRepository->getOrdersForStoreIds($store, $request);

        return $this->response(compact('orders'));
    }

    /**
     *  查询当个订单的详情
     * @param $trad_no
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     * @throws \Throwable
     */
    public function show($trad_no)
    {
        /** @var Store $store */
        $store = Guard::store();

        $youzanAuthorizer = $store->youzanAuthorizer;

        $this->checkIsTrue($youzanAuthorizer,trans('admin.no_auth_to_youzan'));

        $order = (new Order)->where('trad_no', $trad_no)->first();
        $this->checkItemExist($order);

        $order_info = $this->orderRepository->getTrade($youzanAuthorizer, ['tid' => $trad_no]);

        /** 处理订单退款的状态 */
        $order_info = $this->orderRepository->transformOrderRefundInfo($order_info);

        return $this->response(compact('order_info'));
    }
}